dwr.engine.setActiveReverseAjax(true);

function addGroup() {
    var groupName = dwr.util.byId("groupNameInput").value;
    var desc = dwr.util.byId("groupDescriptionInput").value;
    if(groupName.length==0 || desc.length ==0) {
        alert("Please provide name and description of group");
        return;
    }
    var newGroup = new ProductGroup();
    newGroup.name = groupName;
    newGroup.description = desc;
    GroupService.saveGroup(newGroup, function(id) {
//====================Now we use reverse ajax==================================
        /*newGroup.id = id;
        var data = new Array();
        data.push(newGroup);
        dwr.util.addRows( "groupTbody",
            data,
            [
                function(group) {return "<input type='checkbox' id='"+group.id+"' onclick='processGroupCheckbox(this);'/>"},
                function(group) {return group.id;},
                function(group) {return group.name;},
                function(group) {return group.description;}
            ],
            {escapeHtml:false}
       );*/
    });
    dwr.util.byId("groupNameInput").value = "";
    dwr.util.byId("groupDescriptionInput").value="";
}

function showGroups() {
    dwr.util.byId("groupDiv").style.display= "none";
    dwr.util.byId("productsDiv").style.display= "none";
    dwr.util.removeAllRows('groupTbody');
    GroupService.getGroups(function(data){
       if(data) {
           dwr.util.addRows( "groupTbody",
                data,
                [
                    function(group) {return "<input type='checkbox' id='"+group.id+"' onclick='processGroupCheckbox(this);'/>"},
                    function(group) {return group.id;},
                    function(group) {return group.name;},
                    function(group) {return group.description;}
                ],
                {escapeHtml:false}
           );
       } 
    });
    dwr.util.byId("groupDiv").style.display= "block";
}

var checkedGroups = {};

function processGroupCheckbox(checkbox) {
    if(checkbox.checked) {
        checkedGroups[checkbox.id] =true;
    } else {
        checkedGroups[checkbox.id] =false;
    }
}

function deleteCheckedGroups() {
    console.log(checkedGroups);
    var arr = new Array();
    for(var key in checkedGroups) {
        if(checkedGroups[key]) {
            arr.push(key);
        }
        GroupService.deleteGroups(arr);
    }
    showGroups();
}

var groups = new Array();

function showProducts() {
    dwr.util.byId("groupDiv").style.display= "none";
    dwr.util.byId("productsDiv").style.display= "none";
    dwr.util.removeAllRows('productTbody');
//    dwr.util.removeAllOptions('productGroupSelect')        
    GroupService.getGroups(function(data){
//        dwr.util.addOptions("productGroupSelect", data, "id", "name");
        groups = data;
    });
    
    ProductService.getProducts(function(data) {
        if(data) {
            dwr.util.addRows( "productTbody",
                data,
                [
                    function(product) {return "<input type='checkbox' id='"+product.id+"' onclick='processProductCheckbox(this);'/>"},
                    function(product) {return product.id;},
                    function(product) {return "<span title='"+product.description+"'>"+product.name+"</span>";},
                    function(product) {return product.price;},
                    function(product) {
                        if(product.lastmodified) {
                            var d= product.lastmodified;
                            var curr_date = d.getDate();
                            var curr_month = d.getMonth();
                            var curr_year = d.getFullYear();
                            return (curr_date + "-" + curr_month + "-" + curr_year);
                        } else return product.lastmodified;
                    },
                    function(product) {return product.group.name;}
                ],
                {escapeHtml:false}
           );
        }
    });

    dwr.util.byId("productsDiv").style.display= "block";   
}

function addProduct() {
    var productname = dwr.util.byId("productNameInput").value;
    var desc = dwr.util.byId("productDescriptionInput").value;
    var price = dwr.util.byId("productPriceInput").value;
    var groupId = dwr.util.byId("productGroupSelect").value;
    
    var p = new Product();
    p.name= productname;
    p.price = price;
    p.description = desc;
    p.lastmodified= new Date();
    if(groups) {
        for(var i=0; i<groups.length; i++) {
           if(groups[i].id == groupId) {
               p.group = groups[i];
           }
        }  
    }
    ProductService.saveProduct(p, function(id) {
        p.id = id;
        var data = new Array();
        data.push(p);
        dwr.util.addRows( "productTbody",
                data,
                [
                    function(product) {return "<input type='checkbox' id='"+product.id+"' onclick='processProductCheckbox(this);'/>"},
                    function(product) {return product.id;},
                    function(product) {return "<span title='"+product.description+"'>"+product.name+"</span>";},
                    function(product) {return product.price;},
                    function(product) {
                        if(product.lastmodified) {
                            var d= product.lastmodified;
                            var curr_date = d.getDate();
                            var curr_month = d.getMonth();
                            var curr_year = d.getFullYear();
                            return (curr_date + "-" + curr_month + "-" + curr_year);
                        } else return product.lastmodified;
                    },
                    function(product) {return product.group.name;}
                ],
                {escapeHtml:false}
           );
    });
    
    dwr.util.byId("productNameInput").value="";
    dwr.util.byId("productDescriptionInput").value="";
    dwr.util.byId("productPriceInput").value="";
    dwr.util.byId("productGroupSelect").value="";
}